#coding=utf-8
#Boa:Dialog:DlgCustRegion

import wx
from ctrlAider import MyListCtrlDecorator
from product import CDataCustomerRegion

from MyListCtrl import MySimpleListCtrl

def create(parent):
    return DlgCustRegion(parent)

[wxID_DLGCUSTREGION, wxID_DLGCUSTREGIONBTNDEL, wxID_DLGCUSTREGIONBTNEDIT, 
 wxID_DLGCUSTREGIONBTNNEW, wxID_DLGCUSTREGIONBTNSEARCH, 
 wxID_DLGCUSTREGIONTCFILTER, 
] = [wx.NewId() for _init_ctrls in range(6)]

class DlgCustRegion(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGCUSTREGION, name=u'DlgCustRegion',
              parent=prnt, pos=wx.Point(381, 211), size=wx.Size(750, 540),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u5730\u533a\u5206\u7c7b')
        self.SetClientSize(wx.Size(742, 506))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))
        self.Center(wx.BOTH)

        self.btnNew = wx.Button(id=wxID_DLGCUSTREGIONBTNNEW,
              label=u'\u65b0\u5efa', name=u'btnNew', parent=self,
              pos=wx.Point(280, 16), size=wx.Size(86, 29), style=0)
        self.btnNew.Bind(wx.EVT_BUTTON, self.OnBtnNewButton,
              id=wxID_DLGCUSTREGIONBTNNEW)

        self.tcFilter = wx.TextCtrl(id=wxID_DLGCUSTREGIONTCFILTER,
              name=u'tcFilter', parent=self, pos=wx.Point(8, 16),
              size=wx.Size(160, 28), style=0, value=u'')

        self.btnSearch = wx.Button(id=wxID_DLGCUSTREGIONBTNSEARCH,
              label=u'\u641c\u7d22', name=u'btnSearch', parent=self,
              pos=wx.Point(168, 16), size=wx.Size(86, 29), style=0)
        self.btnSearch.SetDefault()
        self.btnSearch.Bind(wx.EVT_BUTTON, self.OnBtnSearchButton,
              id=wxID_DLGCUSTREGIONBTNSEARCH)

        self.btnEdit = wx.Button(id=wxID_DLGCUSTREGIONBTNEDIT,
              label=u'\u4fee\u6539', name=u'btnEdit', parent=self,
              pos=wx.Point(376, 16), size=wx.Size(86, 29), style=0)
        self.btnEdit.Bind(wx.EVT_BUTTON, self.OnBtnEditButton,
              id=wxID_DLGCUSTREGIONBTNEDIT)

        self.btnDel = wx.Button(id=wxID_DLGCUSTREGIONBTNDEL,
              label=u'\u5220\u9664', name=u'btnDel', parent=self,
              pos=wx.Point(472, 16), size=wx.Size(86, 29), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_DLGCUSTREGIONBTNDEL)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        cols = [(CDataCustomerRegion.fi_id, wx.LIST_FORMAT_LEFT, -1),
                (CDataCustomerRegion.fi_name, wx.LIST_FORMAT_LEFT, 150),
                (CDataCustomerRegion.fi_code, wx.LIST_FORMAT_LEFT, 100),
                (CDataCustomerRegion.fi_memo, wx.LIST_FORMAT_LEFT, 300)]
                
        #self.listCtrl  = MyListCtrlDecorator(self.lc, cols, CDataCustomerRegion)
        
        self.lc = MySimpleListCtrl(self, CDataCustomerRegion, cols)
        self.lc.Move(wx.Point(8, 56))
        self.lc.SetSize(wx.Size(728, 432))
        self.lc.refresh()
        self.lc.Bind(wx.EVT_LEFT_DCLICK, self.OnLcLeftDclick)

    def getSelectObj(self):
        return CDataCustomerRegion.getFirstInstance({CDataCustomerRegion.fi_id:self.m_regionId})
        
    def OnLcLeftDclick(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        self.m_regionId = int(self.lc.GetItemText(item))
        
        self.EndModal(wx.ID_OK)

    def OnBtnNewButton(self, event):
        import DlgCustRegionEditor
        
        dlg = DlgCustRegionEditor.create(self)
        if (dlg.ShowModal() == wx.ID_OK):
            self.lc.refresh()

    def OnBtnSearchButton(self, event):
        if not self.tcFilter.GetValue():
            return
        
        self.lc.refreshView(self.tcFilter.GetValue())

    def OnBtnEditButton(self, event):
        import DlgCustRegionEditor
        
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        dlg = DlgCustRegionEditor.create(self)
        dlg.setEditData(CDataCustomerRegion.findObj(int(self.lc.GetItemText(item))))
        if (dlg.ShowModal() == wx.ID_OK):
            self.lc.refresh()

    def OnBtnDelButton(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        dlg = wx.MessageDialog(self, u'是否删除该项?', u'提示',
                                    wx.YES_NO|wx.ICON_QUESTION)
        if(dlg.ShowModal() == wx.ID_YES):
            obj = CDataCustomerRegion.findObj(int(self.lc.GetItemText(item)))
            obj.delete()
            self.lc.refresh()
        
